package com.ktg.mes.qc.controller;

import java.lang.reflect.Array;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktg.common.core.domain.APIResult;
import com.ktg.mes.qc.domain.*;
import com.ktg.mes.qc.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.core.domain.Result;
import com.ktg.common.utils.poi.ExcelUtil;

/**
 * 检测模板-产品检测项Controller
 *
 * @author byteco
 * @date 2022-12-06
 */
@RestController
@RequestMapping("/qc/qcItemTemplateIndex")
public class QcItemTemplateIndexController extends BaseController
{
    @Autowired
    private IQcItemTemplateService qcItemTemplateService;
    @Autowired
    private IQcItemTemplateIndexService qcItemTemplateIndexService;

    @Autowired
    private IQcTemplateService templateService;



    @Autowired
    private IQcTemplateProductService templateProductService;

    /**
     * 查询检测模板-产品检测项列表
     */
    @PreAuthorize("@ss.hasPermi('qc:qcItemTemplateIndex:list')")
    @GetMapping("/list")
    public Result<IPage<QcItemTemplateIndex>> list(QcItemTemplateIndex qcItemTemplateIndex,
                                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                            HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("查询检测模板-产品检测项列表",req,null);
        try {
            Page<QcItemTemplateIndex> page = new Page<QcItemTemplateIndex>(pageNo, pageSize);
            IPage<QcItemTemplateIndex> pageList = qcItemTemplateIndexService.queryPageList(qcItemTemplateIndex,page);
            return apiLog.ok(pageList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 导出检测模板-产品检测项列表
     */
    @PreAuthorize("@ss.hasPermi('qc:qcItemTemplateIndex:export')")
    @Log(title = "检测模板-产品检测项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QcItemTemplateIndex qcItemTemplateIndex,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("导出检测模板-产品检测项列表",req,qcItemTemplateIndex);
        try {
            List<QcItemTemplateIndex> list = qcItemTemplateIndexService.queryList(qcItemTemplateIndex);
            ExcelUtil<QcItemTemplateIndex> util = new ExcelUtil<QcItemTemplateIndex>(QcItemTemplateIndex.class);
            util.exportExcel(response, list, "检测模板-产品检测项数据");
            apiLog.ok(null);
        }catch (Exception e){
            apiLog.error(e);
        }
    }

    /**
     * 获取检测模板-产品检测项详细信息
     */
    @PreAuthorize("@ss.hasPermi('qc:qcItemTemplateIndex:query')")
    @GetMapping(value = "/{recordId}")
    public Result<QcItemTemplateIndex> getInfo(@PathVariable("recordId") Long recordId,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("获取检测模板-产品检测项详细信息",req,null);
        try {
            return apiLog.ok(qcItemTemplateIndexService.getInfo(recordId));
        }catch (Exception e){
            return apiLog.error(e);
        }
    }


    /**
     * 获取检测模板-产品检测项详细信息
     */
    @PreAuthorize("@ss.hasPermi('qc:qcItemTemplateIndex:query')")
    @GetMapping(value = "/getTemplateIndexList/{itemId}/{templateId}/{itemTemplateId}")
    public Result<QcItemTemplate> getTemplateIndexList(@PathVariable(value = "itemId",required = true) Long itemId, //产品id
                                                        @PathVariable(value = "templateId",required = true) Long templateId, //检测模板
                                                        @PathVariable(value = "itemTemplateId",required = true) Long itemTemplateId, //检测结果单头
                                                        HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("获取检测模板-产品检测汇总详细信息",req,null);
        try {

            //产品检测信息
            QueryWrapper<QcTemplateProduct> qcTemplateProductQueryWrapper = new QueryWrapper<>();
            qcTemplateProductQueryWrapper.eq("item_id",itemId);
            QcTemplateProduct templateProduct = templateProductService.getById(templateId);
            if (templateProduct == null){
                return apiLog.fail("当前生产的产品未配置此类型的检验模板！");
            }

            //产品检测信息
            QcTemplate template = templateService.getById(templateId);
            if (template == null){
                return apiLog.fail("当前生产的产品未配置此类型的检验模板！");
            }

            //产品检测信息
            QcItemTemplate itemTemplate = qcItemTemplateService.getById(itemTemplateId);
            if (itemTemplate == null){
                return apiLog.fail("当前生产的产品未配置此类型的检验模板！");
            }

            //产品检测信息
            QcItemTemplateIndex itemTemplateIndex = new QcItemTemplateIndex();
            itemTemplateIndex.setItemTemplateId(itemTemplateId);
            QueryWrapper<QcItemTemplateIndex>itemTemplateQueryWrapper = new QueryWrapper<>(itemTemplateIndex);
            List<QcItemTemplateIndex> indexList = qcItemTemplateIndexService.list(itemTemplateQueryWrapper);

            return apiLog.ok(indexList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }


    /**
     * 新增检测模板-产品检测项
     */
    @PreAuthorize("@ss.hasPermi('qc:qcItemTemplateIndex:add')")
    @Log(title = "检测模板-产品检测项", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<String> add(@RequestBody QcItemTemplateIndex qcItemTemplateIndex,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("新增检测模板-产品检测项",req,qcItemTemplateIndex);
        try {
            qcItemTemplateIndexService.insert(qcItemTemplateIndex);
            return apiLog.ok("添加成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 修改检测模板-产品检测项
     */
    @PreAuthorize("@ss.hasPermi('qc:qcItemTemplateIndex:edit')")
    @Log(title = "检测模板-产品检测项", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public Result<String> edit(@RequestBody QcItemTemplateIndex qcItemTemplateIndex,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("修改检测模板-产品检测项",req,qcItemTemplateIndex);
        try {
            qcItemTemplateIndexService.update(qcItemTemplateIndex);
            return apiLog.ok("编辑成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 删除检测模板-产品检测项
     */
    @PreAuthorize("@ss.hasPermi('qc:qcItemTemplateIndex:remove')")
    @Log(title = "检测模板-产品检测项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public Result<String> remove(@PathVariable Long[] recordIds,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("删除检测模板-产品检测项",req,null);
        try {
            qcItemTemplateIndexService.delete(recordIds);
            return apiLog.ok("删除成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }

    }
}
